package io.framework.modules.hpinfo.controller;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import io.framework.common.annotation.LogOperation;
import io.framework.common.constant.Constant;
import io.framework.common.exception.ErrorCode;
import io.framework.common.page.PageData;
import io.framework.common.utils.Result;
import io.framework.common.validator.AssertUtils;
import io.framework.common.validator.ValidatorUtils;
import io.framework.common.validator.group.AddGroup;
import io.framework.common.validator.group.DefaultGroup;
import io.framework.common.validator.group.UpdateGroup;
import io.framework.modules.hpinfo.dto.VTHpglJsdwinfoDTO;
import io.framework.modules.hpinfo.service.VTHpglJsdwinfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 环境影响评价管理-环评建设单位信息
 *
 * @author Gujq email
 * @since 1.0.0 2020-03-19
 */
@RestController
@Api(tags="环境影响评价管理-环评建设单位信息")
public class VTHpglJsdwinfoController {
    @Autowired
    private VTHpglJsdwinfoService vTHpglJsdwinfoService;

    @GetMapping("hpinfo/vthpgljsdwinfo/page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    public Result<PageData<VTHpglJsdwinfoDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){

        PageData<VTHpglJsdwinfoDTO> page = vTHpglJsdwinfoService.pages(params);

        return new Result<PageData<VTHpglJsdwinfoDTO>>().ok(page);
    }

    @GetMapping("hpinfo/vthpgljsdwinfo/{id}")
    @ApiOperation("信息")
    public Result<VTHpglJsdwinfoDTO> get(@PathVariable("id") String id){

        VTHpglJsdwinfoDTO data = vTHpglJsdwinfoService.selectById(id);

        return new Result<VTHpglJsdwinfoDTO>().ok(data);
    }

    @GetMapping("hpinfo/vthpgljsdwinfo/getProjectInfoByProjectId/{id}")
    @ApiOperation("根据建设项目ID查询建设单位信息")
    public Result<VTHpglJsdwinfoDTO> getProjectInfoByProjectId(@PathVariable("id") String id){

        VTHpglJsdwinfoDTO data = vTHpglJsdwinfoService.getProjectInfoByProjectId(id);

        return new Result<VTHpglJsdwinfoDTO>().ok(data);
    }

    @GetMapping("hpinfo/vthpgljsdwinfo/getAllInfoToSelect")
    @ApiOperation("获取所有建设单位名称  --下拉框数据")
    public Result<List<Map<String, Object>>> getAllInfoToSelect(@ApiIgnore @RequestParam Map<String, Object> params) {

        List<Map<String, Object>> list= vTHpglJsdwinfoService.getAllInfoToSelect(params);

        return new Result<List<Map<String, Object>>>().ok(list);
    }

    @GetMapping("hpinfo/vthpgljsdwinfo/bindJsdwInfo")
    @ApiOperation("绑定建设单位信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pkId", value = "环评建设单位 ID", paramType = "query", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "projectId", value = "绑定环评建设项目ID", paramType = "query", required = true, dataType = "Long"),
    })
    public Result bind(@ApiIgnore @RequestParam Map<String, Object> params) {

        int flag = vTHpglJsdwinfoService.bindJsdwInfo(params);

        if (flag < 1) {
            return new Result().error(ErrorCode.UPDATE_INFO_ERROR);
        }
        return new Result();
    }

    @PostMapping("hpinfo/vthpgljsdwinfo/save")
    @ApiOperation("保存")
    @LogOperation("保存")
    public Result save( VTHpglJsdwinfoDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        dto.setPkId(IdWorker.get32UUID());
        dto.setCreatetime(new Date());

        vTHpglJsdwinfoService.save(dto);

        return new Result();
    }

    @PutMapping("hpinfo/vthpgljsdwinfo/update")
    @ApiOperation("修改")
    @LogOperation("修改")
    public Result update(VTHpglJsdwinfoDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        dto.setUpdatedate(new Date());
        vTHpglJsdwinfoService.update(dto);

        return new Result();
    }

    @DeleteMapping("hpinfo/vthpgljsdwinfo/delete/{id}")
    @ApiOperation("删除")
    @LogOperation("删除")
    public Result delete(@PathVariable("id") String id){
        //效验数据
        AssertUtils.isBlank("id",ErrorCode.IDENTIFIER_NOT_NULL);

        vTHpglJsdwinfoService.deleteById(id);

        return new Result();
    }

}